// src/services/bookServiceFactory.js
import { Book } from '@/models/Book'
import api from '@/services/api'

export const createBookService = () => {
  console.log('🔌 使用真实API模式')

  return {
    async getAllBooks() {
      const response = await api.get('/api/v1/books')
      return response.map(book => new Book(book))
    },

    async getBookById(id) {
      const response = await api.get(`/api/v1/books/${id}`)
      return new Book(response)
    },

    async getBookByIsbn(isbn) {
      const response = await api.get(`/api/v1/books/isbn/${isbn}`)
      return new Book(response)
    },

    async getBooksByAuthor(author) {
      const response = await api.get(`/api/v1/books/author/${author}`)
      return response.map(book => new Book(book))
    },

    async getBooksByGenre(genre) {
      const response = await api.get(`/api/v1/books/genre/${genre}`)
      return response.map(book => new Book(book))
    },

    async createBook(bookData) {
      const response = await api.post('/api/v1/books', bookData)
      return new Book(response)
    },

    async borrowBook(bookId) {
      const response = await api.post(`/api/v1/books/${bookId}/borrow`)
      return new Book(response)
    },

    async returnBook(bookId) {
      const response = await api.post(`/api/v1/books/${bookId}/return`)
      return new Book(response)
    },

    async deleteBook(bookId) {
      await api.delete(`/api/v1/books/${bookId}`)
    }
  }
}